const {db} = require('../../config/dbconfig')

exports.addAllCommentlikes = async (req, res, next) => {
    const {comment_id, user_id} = req.body;

    if (!comment_id || !user_id) {
        return res.status(400).json({error: 'Comment ID and User ID are required'});
    }

    const sql = 'INSERT INTO CommentLikes (comment_id, user_id) VALUES (?, ?)';
    db.query(sql, [comment_id, user_id], (error, results) => {
        if (error) {
            console.error('Error adding comment like:', error);
            return res.status(500).json({error: 'Error adding comment like'});
        }

        const updateSql = 'UPDATE Comments SET likes = likes + 1 WHERE id = ?';
        db.query(updateSql, [comment_id], (error, results) => {
            if (error) {
                console.error('Error updating comment likes:', error);
                return res.status(500).json({error: 'Error updating comment likes'});
            }

            res.status(201).json({message: 'Comment like added successfully'});
        });
    });
}

exports.deleteCommentlikes = async (req, res, next) => {
    const commentId = req.params.commentId;
    const userId = req.params.userId;

    const deleteSql = 'DELETE FROM CommentLikes WHERE comment_id = ? AND user_id = ?';
    db.query(deleteSql, [commentId, userId], (error, results) => {
        if (error) {
            console.error('Error deleting comment like:', error);
            return res.status(500).json({error: 'Error deleting comment like'});
        }

        const updateSql = 'UPDATE Comments SET likes = likes - 1 WHERE id = ?';
        db.query(updateSql, [commentId], (error, results) => {
            if (error) {
                console.error('Error updating comment likes:', error);
                return res.status(500).json({error: 'Error updating comment likes'});
            }

            res.json({message: 'Comment like deleted successfully'});
        });
    });
}

exports.getAllCommentlikes = async (req, res, next) => {
    // 查询所有评论的点赞信息
    const sql = `
    SELECT CommentLikes.comment_id, CommentLikes.user_id, Users.username, Comments.content
    FROM CommentLikes
    INNER JOIN Users ON CommentLikes.user_id = Users.id
    INNER JOIN Comments ON CommentLikes.comment_id = Comments.id
  `;
    db.query(sql, (error, results) => {
        if (error) {
            console.error('Error fetching comment likes:', error);
            return res.status(500).json({error: 'Error fetching comment likes'});
        }

        // 将点赞信息按评论分组
        const commentsWithLikes = {};
        results.forEach(result => {
            const {comment_id, user_id, username, content} = result;
            if (!commentsWithLikes[comment_id]) {
                commentsWithLikes[comment_id] = {
                    "comment_id": comment_id,
                    "content": content,
                    "user": []
                };
            }
            commentsWithLikes[comment_id]["user"].push({user_id, username});
        });

        // 将结果转换为数组形式
        const commentLikesArray = Object.values(commentsWithLikes);

        res.json(commentLikesArray);
    });
}

exports.getCommentlikes = async (req, res, next) => {
    const commentId = req.params.commentId;
    const sql = `
    SELECT CommentLikes.comment_id, CommentLikes.user_id, Users.username
    FROM CommentLikes
    INNER JOIN Users ON CommentLikes.user_id = Users.id
    WHERE CommentLikes.comment_id = ?
  `;
    db.query(sql, [commentId], (error, results) => {
        if (error) {
            console.error('Error fetching comment likes:', error);
            return res.status(500).json({error: 'Error fetching comment likes'});
        }

        res.json(results);
    });
}

